.data 
clear: .int 0,0,0,0
array: .int 31,76,52,91,6,71,5,31,26,1,12,60,87,43,50,39
len=.-array
l:.int 0,0
r:.int 0,0
msg1:.ascii "array before sort:"
len2=.-msg1
msg2:.ascii "array after  sort:"
len3=.-msg2
msg3:.ascii ""
msg4:.ascii ""


.text
.global __main
.type   __main,%function
__main:
	mov x0,#0
        ldr x1,=msg1
        mov x2,len2
        mov x8,64
        svc #0

        bl  getmsg3

        mov x0,#0
	ldr x1,=msg3
	mov x2,x12
	mov x8,64
	svc #0

        mov w0,len
	sub w0,w0,#4
	ldr x1,=r
	str w0,[x1]

        bl  qsort

        mov x0,#0
	ldr x1,=msg2
	mov x2,len3
	mov x8,64
	svc #0

        bl  getmsg4

        mov x0,#0
	ldr x1,=msg4
	mov x2,x12
	mov x8,64
	svc #0
	
	mov x0,123
        mov x8,93
        svc #0

.text
.global selection
.type   selection,%function
selection:
        stp x0,x1,[sp,-16]!
        stp x2,x3,[sp,-16]!
	stp x4,x5,[sp,-16]!
	stp x6,x7,[sp,-16]!
	stp x8,x9,[sp,-16]!
     
        ldr x0,=array
        mov w8,len
        sub w9,w8,#4

        mov w1,#0
        mov w2,#0

lt1:   add w2,w1,#4
       mov w3,w1
       add x3,x3,x0
lt2:   ldr w5,[x3]
        mov w4,w2
        add x4,x4,x0
        ldr w6,[x4]

        cmp w5,w6
        ble  bac3
        str w5,[x4]
        str w6,[x3]

bac3:   add w2,w2,#4
        cmp w2,w8
        blt lt2

        add w1,w1,#4
        cmp w1,w9
        blt lt1

        ldp x8,x9,[sp],16
	ldp x6,x7,[sp],16
	ldp x4,x5,[sp],16
	ldp x2,x3,[sp],16
        ldp x0,x1,[sp],16
	ret

.text
.global bubble
.type   bubble,%function
bubble:
        stp x0,x1,[sp,-16]!
        stp x2,x3,[sp,-16]!
	stp x4,x5,[sp,-16]!
	stp x6,x7,[sp,-16]!
	stp x8,x9,[sp,-16]!
	stp x10,x11,[sp,-16]!
	stp x29,x30,[sp, -16]!
        ldr x1,=array

        mov w10,len
        sub w9,w10,#4
	mov w10,w9

        mov w3,#0
        mov w4,#0
	ldr x2,=array   


lp1:	mov x2,x1       
	sub x2,x2,#4
	eor w4,w4,w4

lp2:    ldr w5,[x2,#4]! 
        ldr w6,[x2,#4]             
        cmp w5,w6
        bgt swap



bak2:   add w4,w4,#4 
	cmp w4,w10           
        blt lp2
bak1:   add w3,w3,#4
        sub w10,w10,#4
	cmp w3,w9
        blt lp1

        ldp x29,x30,[sp],16
	ldp x10,x11,[sp],16
	ldp x8,x9,[sp],16
	ldp x6,x7,[sp],16
	ldp x4,x5,[sp],16
	ldp x2,x3,[sp],16
        ldp x0,x1,[sp],16
	ret

swap:	str w6,[x2] 
        str w5,[x2,#4]
	bl  bak2

.text
.global qsort
.type   qsort,%function
qsort:    
	stp x0,x1,[sp,-16]!
        stp x2,x3,[sp,-16]!
	stp x4,x5,[sp,-16]!
	stp x6,x7,[sp,-16]!
	stp x8,x9,[sp,-16]!
	stp x10,x11,[sp,-16]!
	stp x12,x13,[sp,-16]!
	stp x29,x30,[sp, -16]!
        add x29,sp,0

	ldr x2,=l
	ldr x4,=r
	ldr x6,=array
	ldr w3,[x2]
	ldr w5,[x4]
	ldr w7,[x6]

	add x0,x3,x5
	bl  del
	add x1,x1,x6
	ldr w0,[x1]

lop0:
lop1:   add x8,x3,x6
	ldr w9,[x8]
	cmp x9,x0
	bge lop2
	add x3,x3,#4
	b   lop1
lop2:   add x10,x5,x6
        ldr w11,[x10]
	cmp x11,x0
	ble jud
	sub x5,x5,#4
	b   lop2
jud:    cmp x3,x5
        bgt olop0
	str w11,[x8]
	str w9,[x10]
	add x3,x3,#4
	sub x5,x5,#4
olop0:  cmp x3,x5
        ble lop1

        ldr w12,[x2]
        ldr w13,[x4]
qs1:    cmp x12,x5
	bge qs2
	str w5,[x4]
	bl qsort
qs2:    cmp x3,x13
	bge lp
	str w3,[x2]
	str w13,[x4]
	bl qsort
	
lp:	ldp x29,x30,[sp],16
        ldp x12,x13,[sp],16
	ldp x10,x11,[sp],16
	ldp x8,x9,[sp],16
	ldp x6,x7,[sp],16
	ldp x4,x5,[sp],16
	ldp x2,x3,[sp],16
        ldp x0,x1,[sp],16
	
	ret

.global del
.type   del,%function
del:    stp x2,x3,[sp,-16]!
        eor x3,x3,x3
        mov x2,x0
lop:    sub x2,x2,#8
        add x3,x3,#4
        cmp x2,#2
        bgt lop
        mov x1,x3
        ldp x2,x3,[sp],16
        ret

.text
.global getmsg3
.type   getmsg3,%function
getmsg3: 
        stp x0,x1,[sp,-16]!
        stp x2,x3,[sp,-16]!
        stp x4,x5,[sp,-16]!
        stp x6,x7,[sp,-16]!
        stp x8,x9,[sp,-16]!
        stp x10,x11,[sp,-16]!
	stp x15,x16,[sp,-16]!
	stp x17,x18,[sp,-16]!
	stp x19,x20,[sp,-16]!
	stp x29,x30,[sp, -16]!

	mov x10,len
	mov x15,#10
	mov x18,#32
	mov x17,#256
	mov x16,#48

	ldr x0,=array
	eor x2,x2,x2

	ldr x5,=msg4
	eor x7,x7,x7
	
	eor x8,x8,x8
	eor x21,x21,x21
	str x21,[x5,x7]

lt30:	ldr w1,[x0,x2]
	bl  del2

	cmp x19,#0
	beq lt32
	eor x6,x6,x6
	ldr x6,[x5,x7]
	cmp x8,#0
	beq lt31
	add x19,x19,x16
	mul x19,x19,x17
	add x19,x19,x6
	str x19,[x5,x7]
	mov x8,#0
	add x7,x7,#2
	eor x21,x21,x21
	str x21,[x5,x7]
	eor x19,x19,x19
	b   lt32
lt31:   add x19,x19,x16
	add x19,x19,x6
	str x19,[x5,x7]
	eor x19,x19,x19
	add x8,x8,#1

lt32:   cmp x20,#10
	beq lt33
	eor x6,x6,x6
	ldr x6,[x5,x7]
	cmp x8,#0
	beq lt322
	add x20,x20,x16
	mul x20,x20,x17
	add x20,x6,x20
	str x20,[x5,x7]
	mov x8,#0
	add x7,x7,#2
	eor x21,x21,x21
	str x21,[x5,x7]
	mov x20,#10
	b   lt33
lt322:  add x20,x20,x16
	add x20,x20,x6
	str x20,[x5,x7]
	mov x20,#10
	add x8,x8,#1

lt33:   ldr x6,[x5,x7]
	cmp x8,#0
	beq lt332
	mul x21,x18,x17
	add x6,x6,x21
	str x6,[x5,x7]
	mov x8,#0
	add x7,x7,#2
	eor x21,x21,x21
	str x21,[x5,x7]
	b   bc34
lt332:  add x6,x6,x18
	str x6,[x5,x7]
	add x8,x8,#1

bc34:   add x2,x2,#4
	cmp x2,x10
	bne lt30
	
	ldr x6,[x5,x7]
	cmp x8,#0
	beq lt34
	mul x21,x15,x17
	add x6,x6,x21
	str x6,[x5,x7]
	mov x8,#0
	add x7,x7,#2
	eor x21,x21,x21
	str x21,[x5,x7]
	b   bc35
lt34:   add x6,x6,x15
	str x6,[x5,x7]
	add x8,x8,#1
	
	
	
bc35:	mov x12,x7
	ldp x29,x30,[sp],16
	ldp x19,x20,[sp],16
	ldp x17,x18,[sp],16
	ldp x15,x16,[sp],16
	ldp x10,x11,[sp],16
        ldp x8,x9,[sp],16
        ldp x6,x7,[sp],16
        ldp x4,x5,[sp],16
        ldp x2,x3,[sp],16
        ldp x0,x1,[sp],16
	ret

.text
.global getmsg4
.type   getmsg4,%function
getmsg4: 
        stp x0,x1,[sp,-16]!
        stp x2,x3,[sp,-16]!
        stp x4,x5,[sp,-16]!
        stp x6,x7,[sp,-16]!
        stp x8,x9,[sp,-16]!
        stp x10,x11,[sp,-16]!
	stp x15,x16,[sp,-16]!
	stp x17,x18,[sp,-16]!
	stp x19,x20,[sp,-16]!
	stp x29,x30,[sp, -16]!

	mov x10,len
	mov x15,#10
	mov x18,#32
	mov x17,#256
	mov x16,#48

	ldr x0,=array
	eor x2,x2,x2

	ldr x5,=msg4
	eor x7,x7,x7
	
	eor x8,x8,x8
	eor x21,x21,x21
	str x21,[x5,x7]

lt40:	ldr w1,[x0,x2]
	bl  del2

	cmp x19,#0
	beq lt42
	eor x6,x6,x6
	ldr x6,[x5,x7]
	cmp x8,#0
	beq lt41
	add x19,x19,x16
	mul x19,x19,x17
	add x19,x19,x6
	str x19,[x5,x7]
	mov x8,#0
	add x7,x7,#2
	eor x21,x21,x21
	str x21,[x5,x7]
	eor x19,x19,x19
	b   lt42
lt41:   add x19,x19,x16
	add x19,x19,x6
	str x19,[x5,x7]
	eor x19,x19,x19
	add x8,x8,#1

lt42:   cmp x20,#10
	beq lt43
	eor x6,x6,x6
	ldr x6,[x5,x7]
	cmp x8,#0
	beq lt422
	add x20,x20,x16
	mul x20,x20,x17
	add x20,x6,x20
	str x20,[x5,x7]
	mov x8,#0
	add x7,x7,#2
	eor x21,x21,x21
	str x21,[x5,x7]
	mov x20,#10
	b   lt43
lt422:  add x20,x20,x16
	add x20,x20,x6
	str x20,[x5,x7]
	mov x20,#10
	add x8,x8,#1

lt43:   ldr x6,[x5,x7]
	cmp x8,#0
	beq lt432
	mul x21,x18,x17
	add x6,x6,x21
	str x6,[x5,x7]
	mov x8,#0
	add x7,x7,#2
	eor x21,x21,x21
	str x21,[x5,x7]
	b   bc44
lt432:  add x6,x6,x18
	str x6,[x5,x7]
	add x8,x8,#1

bc44:   add x2,x2,#4
	cmp x2,x10
	bne lt40
	
	ldr x6,[x5,x7]
	cmp x8,#0
	beq lt44
	mul x21,x15,x17
	add x6,x6,x21
	str x6,[x5,x7]
	mov x8,#0
	add x7,x7,#2
	eor x21,x21,x21
	str x21,[x5,x7]
	b   bc45
lt44:   add x6,x6,x15
	str x6,[x5,x7]
	add x8,x8,#1
	
	
	
bc45:	mov x12,x7
	ldp x29,x30,[sp],16
	ldp x19,x20,[sp],16
	ldp x17,x18,[sp],16
	ldp x15,x16,[sp],16
	ldp x10,x11,[sp],16
        ldp x8,x9,[sp],16
        ldp x6,x7,[sp],16
        ldp x4,x5,[sp],16
        ldp x2,x3,[sp],16
        ldp x0,x1,[sp],16
	ret

.text
.global del2
.type   del2,%function
del2:   
	eor x19,x19,x19
        mov x22,x1
	cmp x22,#10
	blt ld2
ld:     sub x22,x22,#10
        add x19,x19,#1
        cmp x22,#10
        bge ld
        mov x20,x22
	b   re   
ld2:    mov x19,#0
        mov x20,x22
 re:    
	ret
